package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.domain.SysContracts;
import com.ruoyi.system.domain.SysScok;
import com.ruoyi.system.domain.SysUserScok;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysContractsService;
import com.ruoyi.system.service.SysUserScokService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Stock;
import com.ruoyi.system.service.IStockService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 股票列表增删改查Controller
 * 
 * @author majingqi
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/stock1")
public class StockController extends BaseController
{
    @Autowired
    private IStockService stockService;
    @Autowired
    private SysUserScokService sysUserScokService;
    @Autowired
    private SysContractsService contractsService;

    /**
     * 查询股票列表增删改查列表
     */
    @RequiresPermissions("system:stock:list")
    @GetMapping("/list")
    public TableDataInfo list(Stock stock)
    {
        startPage();
        List<Stock> list = stockService.selectStockList(stock);
        return getDataTable(list);
    }

    /**
     * 导出股票列表增删改查列表
     */
    @RequiresPermissions("system:stock:export")
    @Log(title = "股票列表增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Stock stock)
    {
        List<Stock> list = stockService.selectStockList(stock);
        ExcelUtil<Stock> util = new ExcelUtil<Stock>(Stock.class);
        util.exportExcel(response, list, "股票列表增删改查数据");
    }

    /**
     * 获取股票列表增删改查详细信息
     */
    @RequiresPermissions("system:stock:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(stockService.selectStockById(id));
    }

    /**
     * 新增股票列表增删改查
     */
    @RequiresPermissions("system:stock:add")
    @Log(title = "股票列表增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Stock stock)
    {
        return toAjax(stockService.insertStock(stock));
    }

    /**
     * 修改股票列表增删改查
     */
    @RequiresPermissions("system:stock:edit")
    @Log(title = "股票列表增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Stock stock)
    {
        return toAjax(stockService.updateStock(stock));
    }

    /**
     * 删除股票列表增删改查
     */
    @RequiresPermissions("system:stock:remove")
    @Log(title = "股票列表增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(stockService.deleteStockByIds(ids));
    }

    /**
     * 买入
     * @param uid
     * @param code
     * @param num
     * @return
     */
    @RequestMapping("/buy/{uid}/{code}/{num}/{scokname}")
    public AjaxResult buy(@PathVariable Long uid, @PathVariable String code, @PathVariable Integer num
            , @PathVariable String scokname
    ){
        Boolean buy= stockService.buy(uid,code,num,scokname);
        return buy?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 撤销委托：每日收市后，委托全部撤销
     */
    @RequestMapping("/deleteStockData")
    public void deleteStockData(){
        
    }

    /**
     * 撤销个人委托
     * @param sysContracts
     * @return
     */
    @RequestMapping("/closeStock")
    public R closeStock(@RequestBody SysContracts sysContracts){
        contractsService.deleteContracts(sysContracts);
        return R.ok("撤销成功","200");
    }
}
